import Vue from 'vue'
import Router from 'vue-router'
import {Auth} from '../utils/resource'

Vue.use(Router)

const router = new Router({
  routes: [
    {path: '/', redirect: '/login',},
    {path: '/login', component: () => import('../pages/login.vue')},
    {path: '/register', component: () => import('../pages/register.vue')},
    {
      path: '/main', component: () => import('../components/Main.vue'),
      children: [
        {path: '/dashboard', component: () => import('../pages/dashboard.vue')},
        {path: '/user', component: () => import('../pages/user/user.vue')},
        {path: '/user/profile', component: () => import('../pages/user/userProfile.vue')},
        {path: '/user/:id', component: () => import('../pages/user/userDetail.vue')},
        {path: '/user/role/:username', component: () => import('../pages/user/userRole.vue')},
        {path: '/permission', component: () => import('../pages/permission/permission.vue')},
        {path: '/permission/:id/:parent?', component: () => import('../pages/permission/permissionDetail.vue')},
        {path: '/role', component: () => import('../pages/role/role.vue')},
        {path: '/department', component: () => import('../pages/department/department.vue')},
        {path: '/department/:id/:parent?', component: () => import('../pages/department/departmentDetail.vue')},
        {path: '/dict', component: () => import('../pages/dict/dict.vue')},
        {path: '/dict/:code', component: () => import('../pages/dict/dictDetail.vue')},
        {path: '/fileMapping', component: () => import('../pages/filemapping/fileMapping')},
        {path: '/fileMapping/new/:parent/:parentName', component: () => import('../pages/filemapping/fileMappingDetail.vue')},
        {path: '/fileMapping/folder/new/:parent/:parentName', component: () => import('../pages/filemapping/fileMappingFolder.vue')},
        {path: '/fileMapping/folder/:id', component: () => import('../pages/filemapping/fileMappingFolder.vue')},
      ]
    },

  ]
})

// 不需要鉴权的路径
const permissive = ['/login', '/register']

router.beforeEach((to, from, next) => {
  if (permissive.includes(to.path)) {
    return next()
  } else {
    Auth.authorizationCheck().then(res => {
      if (res.data.success) {
        return next()
      } else {
        next({
          path: '/login',
          query: {redirect: to.path}
        })
      }
    })
  }
})

export default router

